package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwCarinfoEditHisVoMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwCarinfoEditVoMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwInterviewInfoVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanEditService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.yw.IYwCarinfoEditHisService;
import com.zjdiepu.www.ibiz.yw.IYwCarinfoEditService;
import com.zjdiepu.www.ibiz.yw.IYwInterviewInfoService;
import com.zjdiepu.www.model.his.dto.HisYwBusinessLoan;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoanEdit;
import com.zjdiepu.www.model.yw.dto.YwCarinfoEdit;
import com.zjdiepu.www.model.yw.dto.YwCarinfoEditHis;
import com.zjdiepu.www.model.yw.dto.YwInterviewInfo;
import com.zjdiepu.www.util.ExceptionHelper;

@Controller
@RequestMapping(value= "YwInterviewInfoViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"面签信息"}, produces="text/html;charset=UTF-8")
public class YwInterviewInfoViewController extends BaseController{

	@Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IYwInterviewInfoService ywInterviewInfoService;
    @Resource
    private IYwCarinfoEditService ywCarinfoEditService;
    @Resource
    private IHisYwBusinessLoanService hisYwBusinessLoanService;
    @Resource
    private IYwBusinessLoanEditService ywBusinessLoanEditService;
    @Resource
    private IYwCarinfoEditHisService ywCarinfoEditHisService;
    @Resource
    private YwCarinfoEditVoMapper ywCarinfoEditVoMapper;
    @Resource
    private YwCarinfoEditHisVoMapper ywCarinfoEditHisVoMapper;
    @Resource
    private AbstractYwBusinessLoanVoNewMapper hisYwBusinessLoanVoMapper;
    @Resource
    private YwInterviewInfoVoMapper ywInterviewInfoVoMapper;
	
	@ApiOperation(value = "二次面签确认", response = Boolean.class, httpMethod="GET", tags={"二次面签确认"})
    @RequestMapping(value = "ywInterviewInfoSecondDetail", method = RequestMethod.GET)
    public String ywInterviewInfoSecondDetail(Model model, String busiId, String interviewInfoId) {
    	if (!StringUtils.isNoneBlank(busiId, interviewInfoId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	viewScopeControllerHelper.preparedBusiness(busiId, model);
    	
    	YwInterviewInfo ywInterviewInfo = ywInterviewInfoService.selectById(interviewInfoId);
    	ywInterviewInfoVoMapper.mapper(ywInterviewInfo);
    	model.addAttribute("ywInterviewInfo", ywInterviewInfo);
    	
    	//车辆信息对比
    	YwCarinfoEdit ywCarinfoEdit = new YwCarinfoEdit();
    	ywCarinfoEdit.setDpStatus(DpStatus.NORMAL.getCode());
    	ywCarinfoEdit.setBusiId(busiId);
    	ywCarinfoEdit.setOrderSql("`CREATED` DESC");
    	ywCarinfoEdit = ywCarinfoEditService.selectRefined(ywCarinfoEdit);
    	if(null != ywCarinfoEdit) {
    		ywCarinfoEditVoMapper.mapper(ywCarinfoEdit);
    		model.addAttribute("ywCarinfoEdit", ywCarinfoEdit);
			YwCarinfoEditHis ywCarinfoEditHis = new YwCarinfoEditHis();
			ywCarinfoEditHis.setDpStatus(DpStatus.NORMAL.getCode());
			ywCarinfoEditHis.setBusiId(busiId);
			ywCarinfoEditHis.setEditId(ywCarinfoEdit.getId());
			ywCarinfoEditHis = ywCarinfoEditHisService.selectRefined(ywCarinfoEditHis);
			ywCarinfoEditHisVoMapper.mapper(ywCarinfoEditHis);
			model.addAttribute("ywCarinfoEditHis", ywCarinfoEditHis);
    	}
    	
    	//贷款信息对比
    	YwBusinessLoanEdit ywBusinessLoanEdit = new YwBusinessLoanEdit();
    	ywBusinessLoanEdit.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessLoanEdit.setBusiId(busiId);
    	ywBusinessLoanEdit.setOrderSql("`CREATED` DESC");
    	ywBusinessLoanEdit = ywBusinessLoanEditService.selectRefined(ywBusinessLoanEdit);
    	if(null != ywBusinessLoanEdit) {
    		model.addAttribute("shenqin", ywBusinessLoanEdit.getCreatorName());
    		model.addAttribute("shenqinDate", DateUtil.dateTo8String(new Date(ywBusinessLoanEdit.getCreated() * 1000L)));
    		hisYwBusinessLoanVoMapper.mapper(ywBusinessLoanEdit); 
    		model.addAttribute("ywBusinessLoanEdit", ywBusinessLoanEdit);
    		
    		String hisLoanId = ywBusinessLoanEdit.getHisLoanId();
    		if(StringUtils.isNotBlank(hisLoanId)) {
    			HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(hisLoanId);
    			if(null != hisYwBusinessLoan) {
//        			直接取值
    				hisYwBusinessLoanVoMapper.mapper(hisYwBusinessLoan);
    				model.addAttribute("hisYwBusinessLoan", hisYwBusinessLoan);
    			}
    		}
    	}
    	return "system/yw/ywInterviewInfoSecondDetail";
    }
}
